package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityOnlineByQuantum;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class OnlineByQuantumManager extends ManagerQueryCacheBase<EntityOnlineByQuantum> {
	private static final long serialVersionUID = 1L;

    /**
     * 得到lookDate，compareDate和对比日七天前的数据
     * @param lookDate
     * @return
     */
    public List<EntityOnlineByQuantum> getOnlineRealTimeDatas(Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
        return findByQuery("from EntityOnlineByQuantum where platformid=:platformid and channelid=:channelid and serverid=:serverid and (day=:dayStart or day=:compareDate or day=:lastWeekDate) order by day desc ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "compareDate", new Date(Time.getBeginningOfDay(compareDate.getTime())), "lastWeekDate", new Date(Time.getBeginningOfDay(lastWeekDate.getTime())), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
    }

	/**
	 * 得到lookDate，和compareDate的数据
	 * @param lookDate
	 * @return
	 */
	public List<EntityOnlineByQuantum> getOnlineRealTimeDatas(Date lookDate, Date compareDate) {
		return findByQuery("from EntityOnlineByQuantum where platformid=:platformid and channelid=:channelid and serverid=:serverid and (day=:dayStart or day=:compareDate)", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "compareDate", new Date(Time.getBeginningOfDay(compareDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
	
	public List<EntityOnlineByQuantum> getOnlineRealTimeDatasByServers(Date lookDate, List<String> servers) {
		if (servers == null || servers.size() <= 0) {
			return null;
		}
		
		return findByQuery("from EntityOnlineByQuantum where day=:dayStart and platformid=:platformid and channelid=:channelid and serverid in (:serverid)", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", servers);
	}

    public List<EntityOnlineByQuantum> getOnlineRealTimeDatasByChannels(Date lookDate, List<String> channels) {
        if (channels == null || channels.size() <= 0) {
            return null;
        }

        return findByQuery("from EntityOnlineByQuantum where day=:dayStart and platformid=:platformid and channelid in (:channelid) and serverid=:serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", channels, "serverid", "0");
    }
	
	// 各服实时在线
	public List<Object[]> getAllOnlineRealTimeDatas(Date lookDate) {
	    String quantum = Time.isToday(lookDate) ? Time.getHHmm() : "23:59";
        return findByQuery("select a.serverid,a.onlinenum,a.channelid from EntityOnlineByQuantum as a where a.quantum<=:quantum and a.day=:dayStart and a.platformid=:platformid and a.channelid=:channelid and a.serverid != :serverid and a.quantum = (select max(quantum) from EntityOnlineByQuantum where quantum<=:quantum and day=:dayStart and platformid=:platformid and channelid=:channelid and serverid != :serverid and a.serverid=serverid and a.quantum=quantum)", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0", "quantum", quantum);
	}
}